package com.chaokang.controller;

import com.chaokang.entity.AnswerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin(origins = "*")
public class AnswerController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${service-url.nacos-user-service}")
    private String answerUrl;
    public HashMap<String,Object> setMapNoId(AnswerEntity answerEntity){
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",null);
        map.put("questionnaire_id",answerEntity.getQuestionnaire_id());
        map.put("user_id",answerEntity.getUser_id());
        map.put("answer",answerEntity.getAnswer());
        map.put("start_time",answerEntity.getStart_time());
        map.put("end_time",answerEntity.getEnd_time());
        map.put("state",answerEntity.getState());
        map.put("submit",answerEntity.getSubmit());
        return map;
    }
    public HashMap<String,Object> setMapWithId(AnswerEntity answerEntity){
        HashMap<String,Object> map = setMapNoId(answerEntity);
        map.put("id",answerEntity.getId());
        return map;
    }

    @PostMapping(value = "/getAnswerByQuestionId",produces = MediaType.APPLICATION_JSON_VALUE)
    public List<AnswerEntity> getAnswerByQuestionId(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAnswerByQuestionId",objectHttpEntity,List.class);
    }
    @PostMapping(value = "getAnswerByQuestionIdAndUserId",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerByQuestionIdAndUserId(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAnswerByQuestionIdAndUserId",objectHttpEntity,AnswerEntity.class);
    }
    @PostMapping(value = "/getAllUserIdByQuestionId",produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Integer> getAllUserIdByQuestionId(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAllUserIdByQuestionId",objectHttpEntity,List.class);
    }
    @PostMapping(value = "/getAnswerById",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerById(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAnswerById",objectHttpEntity,AnswerEntity.class);
    }


    @PostMapping(value = "/insertAnswer",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap insertAnswer(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapNoId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/insertAnswer",objectHttpEntity,HashMap.class);
    }
    @PostMapping(value = "/updateAnswer",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap updateAnswer(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/updateAnswer",objectHttpEntity,HashMap.class);
    }
    @PostMapping(value = "/getAnswerDetailByQuestionnaireId", produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerDetailByQuestionnaireId(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAnswerDetailByQuestionnaireId",objectHttpEntity,AnswerEntity.class);
    }
    @PostMapping(value = "/getAnswerModel",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerModel(@RequestBody AnswerEntity answerEntity){
        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(setMapWithId(answerEntity));
        return restTemplate.postForObject(answerUrl+"/service/getAnswerModel",objectHttpEntity,AnswerEntity.class);
    }
}
